﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FONCREAGROINTEGRAL.Models;
using Microsoft.Reporting.WebForms;

namespace FONCREAGROINTEGRAL.Controllers
{
    public class boletaplanillaController : Controller
    {
        public SistIntFoncreagroEntities entidades = new SistIntFoncreagroEntities();
        //
        // GET: /boletaplanilla/

        public ActionResult Index(int id,int id1)
        {
            string personal = Convert.ToString(Request.QueryString["query"]);

            int start = Convert.ToInt32(Request.QueryString["start"]);
            int limit = Convert.ToInt32(Request.QueryString["limit"]);
            

            if (!String.IsNullOrEmpty(personal))
            {
                var query = entidades.RBOLETAPLANILLA(id,id1).Where(a => (a.Persona).ToUpper().Contains(personal.ToUpper())).Skip(start).Take(limit).ToList();
                return  Json(new { grid = query, total = query.Count() }, JsonRequestBehavior.AllowGet);
            }
            else {
                var query = entidades.RBOLETAPLANILLA(id,id1).Skip(start).Take(limit).ToList();
                return Json(new { grid = query, total = query.Count() }, JsonRequestBehavior.AllowGet);
            }
           
        }

        //
        // GET: /boletaplanilla/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /boletaplanilla/Create

        public ActionResult CrearPlanilla(int id)
        {   
            try{
                var eme = entidades.EJERCICIOMES.Single(em => em.idejerciciomes == id);
                if (eme.vigente == true) {
                    if (eme.estadoP == true) { return Content("2"); }
                    else {
                        entidades.Rllenarboleta(id);
                        return Content("3");//Programar aqui el llenar boleta
                    }
                     }
                else { return Content("1"); }
            }
            catch{
            return Content("0");
            }
            //0 ERROR
            //1 MES NO ESTA VIGENTE
            //2 MES PAGADO NO SE MODIFICA
            //3 SE HA PROCEDIDO A LLENAR LA BOLETA
        } 

        //PagarPlanilla
        public ActionResult PagarPlanilla(int id)
        {
            int id2 = id + 1;
            try
            {
                var eme = entidades.EJERCICIOMES.Single(em => em.idejerciciomes == id);
                if (eme.estadoP == false)
                {
                    eme.estadoP = true;
                    eme.vigente = false;
                    TryUpdateModel(eme);
                    entidades.SaveChanges();

                    var eme2 = entidades.EJERCICIOMES.Single(em => em.idejerciciomes == id2);
                    eme2.vigente = true;
                    TryUpdateModel(eme2);
                    entidades.SaveChanges();
                    entidades.RPAGARPLANILLA(id);
                    return Content("1");
                }
                else { return Content("2");  }
            }
            catch
            {
                return Content("0");
            }
            //0 ERROR
            //1 PAGADO CORRECTAMENTE
            //2 MES YA PAGADO CORRECTAMENTE
        } 

        //
        // POST: /boletaplanilla/Create

        /*[HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }*/
        
        //
        // GET: /boletaplanilla/Edit/5
 
        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /boletaplanilla/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /boletaplanilla/Delete/5
 
        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /boletaplanilla/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        //id ==== idejerciciomes
        public ActionResult ComprobarReport(int id)
        {
            try
            {
                var eme = entidades.EJERCICIOMES.Single(em => em.idejerciciomes == id);
                //if (eme.estadoP == true)
                //{
                //    return Content("2");
                //}
                //else
                //{
                    return Content("2");//anTES 1
                //}
                //return Content("OK");
            }
            catch
            {
                return Content("0");
            }
            //0 error
            //1 mes aun no pagado
        }
        //id ==== idejerciciomes
        public ActionResult Report(int id,int id1)
        {
            try
            {
                var eme = entidades.EJERCICIOMES.Single(em => em.idejerciciomes == id);

                //if (eme.estadoP == true)
                //{
                    LocalReport localReport = new LocalReport();

                    localReport.ReportPath = Server.MapPath("~/Content/Reportes/RBOLETAPLANILLA.rdlc");
                    ReportDataSource reportDataSource = new ReportDataSource("RBOLETAPLANILLA", entidades.RBOLETAPLANILLA(id, id1).ToList());

                    localReport.DataSources.Add(reportDataSource);
                    string reportType = "PDF";
                    string mimeType;
                    string encoding;
                    string fileNameExtension;
                    string deviceInfo =
                     "<DeviceInfo>" +
                  "  <OutputFormat>PDF</OutputFormat>" +
                  "  <PageWidth>8.27in</PageWidth>" +
                  "  <PageHeight>5.83in</PageHeight>" +
                  "  <MarginTop>0in</MarginTop>" +
                  "  <MarginLeft>0in</MarginLeft>" +
                  "  <MarginRight>0in</MarginRight>" +
                  "  <MarginBottom>0in</MarginBottom>" +
                  "</DeviceInfo>";
                    Warning[] warnings;
                    string[] streams;
                    byte[] renderedBytes;
                    renderedBytes = localReport.Render(
                        reportType,
                        deviceInfo,
                        out mimeType,
                        out encoding,
                        out fileNameExtension,
                        out streams,
                        out warnings);
                    Response.Clear();
                    Response.ContentType = mimeType;
                    Response.AddHeader("content-disposition", "attachment; filename=Reportepersona." + fileNameExtension);
                    Response.BinaryWrite(renderedBytes);
                    Response.End();
                    return File(renderedBytes, mimeType);
                //}
                //else
                //{
                //    return Content("1");
                //}
                //return Content("OK");
            }
            catch {
                return Content("0");
            }
            //0 error
            //1 mes aun no pagado
        }
        //id ==== idejerciciomes
        public ActionResult Reportxls(int id,int id1)
        {
            try
            {
                var eme = entidades.EJERCICIOMES.Single(em => em.idejerciciomes == id);

                    LocalReport localReport = new LocalReport();

                    localReport.ReportPath = Server.MapPath("~/Content/Reportes/MRBOLETAPLANILLA.rdlc");
                    ReportDataSource reportDataSource = new ReportDataSource("MRBOLETAPLANILLA", entidades.RBOLETAPLANILLA(id, id1).ToList());

                    localReport.DataSources.Add(reportDataSource);
                    string reportType = "EXCEL";
                    string mimeType;
                    string encoding;
                    string fileNameExtension;
                    string deviceInfo =
                     "<DeviceInfo>" +
                  "  <OutputFormat>EXCEL</OutputFormat>" +
                  "  <PageWidth>8.27in</PageWidth>" +
                  "  <PageHeight>5.83in</PageHeight>" +
                  "  <MarginTop>0in</MarginTop>" +
                  "  <MarginLeft>0in</MarginLeft>" +
                  "  <MarginRight>0in</MarginRight>" +
                  "  <MarginBottom>0in</MarginBottom>" +
                  "</DeviceInfo>";
                    Warning[] warnings;
                    string[] streams;
                    byte[] renderedBytes;
                    renderedBytes = localReport.Render(
                        reportType,
                        deviceInfo,
                        out mimeType,
                        out encoding,
                        out fileNameExtension,
                        out streams,
                        out warnings);
                    Response.Clear();
                    Response.ContentType = mimeType;
                    Response.AddHeader("content-disposition", "attachment; filename=PLANILLA.XLS");
                    Response.BinaryWrite(renderedBytes);
                    Response.End();
                    return File(renderedBytes, mimeType);

                //return Content("OK");
            }
            catch
            {
                return Content("0");
            }
            //0 error
            //1 mes aun no pagado
        }
        //
        // POST: /boletaplanilla/RHAFPGeneral/8
        public ActionResult RHAFPGeneral(int id)
        {
            try
            {
                var eme = entidades.EJERCICIOMES.Single(em => em.idejerciciomes == id);

                LocalReport localReport = new LocalReport();

                localReport.ReportPath = Server.MapPath("~/Content/Reportes/RRHH/AFP/RHAFPGeneral.rdlc");
                ReportDataSource reportDataSource = new ReportDataSource("RHAFPGeneral", entidades.RHAFPGeneral(id).ToList());

                localReport.DataSources.Add(reportDataSource);
                string reportType = "EXCEL";
                string mimeType;
                string encoding;
                string fileNameExtension;
                string deviceInfo =
                 "<DeviceInfo>" +
              "  <OutputFormat>EXCEL</OutputFormat>" +
              "  <PageWidth>8.27in</PageWidth>" +
              "  <PageHeight>5.83in</PageHeight>" +
              "  <MarginTop>0in</MarginTop>" +
              "  <MarginLeft>0in</MarginLeft>" +
              "  <MarginRight>0in</MarginRight>" +
              "  <MarginBottom>0in</MarginBottom>" +
              "</DeviceInfo>";
                Warning[] warnings;
                string[] streams;
                byte[] renderedBytes;
                renderedBytes = localReport.Render(
                    reportType,
                    deviceInfo,
                    out mimeType,
                    out encoding,
                    out fileNameExtension,
                    out streams,
                    out warnings);
                Response.Clear();
                Response.ContentType = mimeType;
                Response.AddHeader("content-disposition", "attachment; filename=AFPGeneral"+Convert.ToString(DateTime.Now)+".XLS");
                Response.BinaryWrite(renderedBytes);
                Response.End();
                return File(renderedBytes, mimeType);

                //return Content("OK");
            }
            catch
            {
                return Content("0");
            }
            //0 error
            //1 mes aun no pagado
        }
    }
}
